/** ************************************************************************************************
 *  GRID
 *
 * TODO
 * - Empty line
 **/
compono.ui.Grid = compono.Class({
	name:"Grid",
	selectionType:false,
	stripes:false,
	commandAll:false,
	commands:false,
	rows:false,
	selectedRows:false,
	emptyRow:false,
	constructor:function(grid)
	{
		this.html = this.$(grid);
		return this;
	},
	event:function()
	{
		if(!this.hasOwnProperty(arguments[0])){
			return; 
		};
		var name = Array.prototype.shift.call(arguments);
		return this[name].apply(this,arguments);
	},
	run:function(grid)
	{
		if(grid){
			this.html = this.$(grid);
		};
		
		if(!this.html){
			return false;
		};
		

		this.getRows();
		

		Array.toArray(this.html.tHead.rows).some(function(tr,index){

			this.$(tr).grid = this;
			
			tr.command = this.$(tr.cells[0]).first();
			
			if(tr.command)
			{
				tr.event("click",function(event){
					this.grid.selectAll(this,event);
				})
				
			};
			
			return this.commandAll = tr.command
			
		},this);

		
		this.commands = this.rows.map(function(tr,index){
		
			this.$(tr).grid = this;
			
			tr.command = this.$(tr.cells[0]).first();
			
			if(tr.command)
			{
				tr.event("click",function(event){
					this.grid.select(this,event);
				});
			};
			
			return tr.command
			
		},this);

		this.getSelectedRows().forEach(function(tr){
			this.select(tr);
		},this);
		
		if(this.stripes)
		{
			this.setStripes();
		};
		
		return this;
	},
	selectAll:function(tr, event)
	{
		if(this.event("onSelectAll", event, tr) == false){
			return false;
		};
		
		Array.toArray(this.html.rows).forEach(function(tr, index){
			if(index > 0)
			{
				this.select(tr, event);
			}
		},this)
	},
	select:function(tr, event, recursion)
	{
	
		if(this.selectionType == "single" && !recursion)
		{
			var commands = this.commands.filter(function(checkbox){
								return checkbox.checked;
							},this)
			
			if(commands.length)
			{
				commands = commands.first();
				
				commands.parentNode.parentNode.check = commands;
				
				if(commands.parentNode.parentNode === tr)
				{
					return;
				};
				
				arguments.callee.call(this, commands.parentNode.parentNode, event, true);
			}
		};

		if(tr.nodeName.toLowerCase() != "tr")
		{
			tr = this.$(tr).parent(function(tr){
					return tr.nodeName.toLowerCase() != "tr"
				})
		};
		
		if(!tr.command)
		{ 
			tr.command = this.$(tr.cells[0]).first(); 
		};

		var selected;
		if(event && event.target){
			selected = event.target === tr.command ? !tr.command.checked : tr.command.checked;
		}else{
			selected = !tr.command.checked;
		}
		
		tr.command.checked = !selected;
		this.$(tr).toggleClassName("selected", null, !selected);

			
		if(!this.selectedRows)
		{
			this.selectedRows = new compono.DOMArray();
		};

		this.selectedRows.push( tr );
		
		if(this.event("onSelect", event, tr) == false)
		{
			tr.command.checked = false;
			this.getSelectedRows();
			
			return false;
		};
		
		return tr;
	},
	getRows:function()
	{
		if(this.$( this.html.tBodies[0].rows[0] ).hasClassName('empty'))
		{
			this.emptyRow = this.html.tBodies[0].rows[0];
			this.rows = new compono.DOMArray(this.html.tBodies[0].rows).slice(1);
		}
		else
		{
			this.rows = new compono.DOMArray(this.html.tBodies[0].rows)
		};
	
	
		return this.rows;
	},
	getSelectedRows:function()
	{
		var selecteds = this.commands.filter(function(checkbox){
										return checkbox.checked;
									},this);
									
		return this.selectedRows = new compono.DOMArray(selecteds).parent().parent();
	},
	addRow:function(values)
	{
		var lastRow = this.getRows().last();
		
		var row = lastRow.cloneNode(true);
		
		this.$(lastRow).parent().append( row );
		
		if(values)
		{
			Array.toArray(row.cells).forEach(function(cell, index)
			{
				if(index > 0){
					cell.innerHTML = values.hasOwnProperty(index - 1) ? values[index - 1] : "";
				}
			})
		};
		
		if(this.stripes)
		{
			this.setStripes();
		};
		
	},
	removeRow:function(row)
	{
		if(!row){
			row = this.getSelectedRows();
		};
		
		Array.toDOMArray(row).remove();
		
		if(this.stripes)
		{
			this.setStripes();
		};
		
	},
	setStripes:function()
	{
		this.getRows().forEach(function(row, index)
		{
			this.$(row).toggleClassName('alternate', null, Boolean(index % 2));
		}, this);
	}
})